// components/dynamicCard/dynamicCard.js
const db = wx.cloud.database();

Component({
    observers:{
        dynamicItem:function(){
            this.dynamicOnshow();
            this.judgeBtn();
        },
    },
    // 在组件实例进入页面节点树时执行
    lifetimes: {
        attached: function () {
          this.dynamicOnshow();
          this.judgeBtn();
        },
    },
    /**
     * 组件的属性列表
     */
    options: {
        multipleSlots: true, // 在组件定义时的选项中启用多slot支持
        styleIsolation: 'isolated'
        //表示启用样式隔离，在自定义组件内外，使用 class 指定的样式将不会相互影响（一般情况下的默认值
    },
    properties: {
        dynamicItem:Object,
        // 判断是从发布页参数设为1还是其他页面来的，其他页面的话此参数为0
        fromPublish:Number,
    },

    /**
     * 组件的初始数据
     */
    data: {
        choices:1,
        likeimg:"../../images/未赞.png",
        collectionimg:"../../images/未收藏.png",
        imgsUrl:[],
        dynamicLarge:0,
        copyContent:"chihuolianmeng",
        dynamicLargeButton:0,
        nickName:"匿名",
        headImg:"https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg",
        // 这个openid是当前动态发布者的openid
        openid:'test',
        // 下面这个openid是登录人的
        userOpenid:'test',
        // 动态的_id
        dynamicId:'test',
        userInfo: null,
    },

    /**
     * 组件的方法列表
     */
    methods: {
        //点击动态文字下面的那个展开的效果
        expand:function() {
            var data=this.data;
            if (data.dynamicLarge==1) {
                this.setData({dynamicLarge:0});
            }else{
                this.setData({dynamicLarge:1});
            }
        },
        // 文件放大需要改urls，key是图片的index从0开始
        imgLarge:function(e) {
            var key=e.currentTarget.dataset.key;
            var data=this.data;
            // console.log(key);
            // console.log(data.imgsUrl[key]);
            wx.previewImage({
              urls:[data.imgsUrl[key]],
              success:(res=>{
                  console.log("图片放大成功");
              })
            })
        },
        //
        likeImg:function() {
            const{userInfo}=this.data
            //没登陆的话直接返回了
            if(!userInfo){
                wx.showToast({
                    icon: 'none',
                    title: '未登录',
                    duration: 1500,
                    success:() => {
                        setTimeout(() => {
                            wx.switchTab({
                                url: '../homepage/homepage',
                            })
                        }, 1500);
                    }
                });    
                return;
            }
            //如果登录的话才给点赞和收藏
            var data=this.data;
            const{dynamicItem}=this.data;
            const{userOpenid}=this.data;
            const{dynamicId}=this.data;
            const{fromPublish}=this.data;
            if (data.likeimg=="../../images/未赞.png") {
                this.setData({likeimg:"../../images/已赞.png"});
                db.collection('like').add({
                    data:{
                        location:dynamicItem.location,
                        name:dynamicItem.name,
                        cuisine:dynamicItem.cuisine,
                        desc:dynamicItem.desc,
                        imgList:dynamicItem.imgList,
                        time:dynamicItem.time, 
                        id:dynamicItem._id,
                        // 这个是这个动态的发布者的openid
                        dynamicOpenid:dynamicItem._openid
                    }
                })
            }else{
                this.setData({likeimg:"../../images/未赞.png"});
                db.collection('like').where({
                    _openid:userOpenid,
                }).where({
                    id:dynamicId
                }).remove({
                    success:function(res){
                        // console.log(res);
                        if(fromPublish==0){
                            wx.reLaunch({
                                url: '/pages/myLike/myLike'
                              })
                        }
                    }
                })
            }
        },
        collectionImg:function() {
            const{userInfo}=this.data
            //没登陆的话直接返回了
            if(!userInfo){
                wx.showToast({
                    icon: 'none',
                    title: '未登录',
                    duration: 1500,
                    success:() => {
                        setTimeout(() => {
                            wx.switchTab({
                                url: '../homepage/homepage',
                            })
                        }, 1500);
                    }
                });    
                return;
            }
            //如果登录的话才给点赞和收藏
            var data=this.data;
            const{dynamicItem}=this.data;
            const{userOpenid}=this.data;
            const{dynamicId}=this.data;
            const{fromPublish}=this.data;
            if (data.collectionimg=="../../images/未收藏.png") {
                this.setData({collectionimg:"../../images/已收藏.png"});
                db.collection('collect').add({
                    data:{
                        location:dynamicItem.location,
                        name:dynamicItem.name,
                        cuisine:dynamicItem.cuisine,
                        desc:dynamicItem.desc,
                        imgList:dynamicItem.imgList,
                        time:dynamicItem.time, 
                        id:dynamicItem._id,
                        // 这个是这个动态的发布者的openid
                        dynamicOpenid:dynamicItem._openid
                    }
                })
            }else{
                this.setData({collectionimg:"../../images/未收藏.png"});
                db.collection('collect').where({
                    _openid:userOpenid,
                }).where({
                    id:dynamicId
                }).remove({
                    success:function(res){
                        // console.log(res);
                        if(fromPublish==0){
                            wx.reLaunch({
                                url: '/pages/myCollection/myCollection'
                              })
                        }
                    }
                })
            }
        },
        copyText(e) {
            let key = e.currentTarget.dataset.key;
            wx.setClipboardData({ //设置系统剪贴板的内容
              data: key,
              success(res) {
                wx.getClipboardData({ // 获取系统剪贴板的内容
                  success(res) {
                    wx.showToast({
                      title: '复制成功',
                    })
                  }
                })
              }
            });
            console.log(this.data.dynamicItem)
          },
            //  这个是组件加载的时候就会执行
          dynamicOnshow:function(){ 
              var that=this
              const{dynamicItem}=this.data
              var dynamicOpenid=dynamicItem._openid
              const{fromPublish}=this.data
              // 动态的原始id
              var dynamicid=dynamicItem._id
              if(fromPublish==0){
                  dynamicOpenid=dynamicItem.dynamicOpenid
                  dynamicid=dynamicItem.id
              }

              
              //获取动态图片
              this.setData({imgsUrl:dynamicItem.imgList})
              //获取头像和昵称
              this.setData({openid:dynamicOpenid},function(){
                  const{openid}=this.data;
                //   console.log(openid)
                //获取头像
                wx.cloud.callFunction({
                    // 云函数名称
                    name: 'getUser',
                    // 传给云函数的参数
                    data: {
                        dynamicOpenid:openid,
                        desc:dynamicItem.desc
                    },
                    success: function(res) {
                    // console.log(res.result.nickName) // 3
                        if(res.result.nickName!="匿名"){
                            // console.log(res.result.nickName)
                            that.setData({nickName:res.result.nickName})
                            that.setData({headImg:res.result.headImg})
                        }
                    },
                    fail: console.error
                })
              }) 

              //获取点赞和收藏的信息
              wx.cloud.callFunction({
                // 云函数名称
                name: 'getOpenid',
                // 传给云函数的参数
                data: {},
                success: function(res) {
                    //获取到了当前登录的人的openid
                    that.setData({userOpenid:res.result.openid},function(){
                        that.setData({dynamicId:dynamicid},function(){
                            const{userOpenid}=that.data
                            const{dynamicId}=that.data
                            wx.cloud.callFunction({
                                // 云函数名称
                                name: 'isLike',
                                // 传给云函数的参数
                                data: {
                                    // 这个传过去的是操作的人的openid
                                    useOpenid:userOpenid,
                                    dynamicId:dynamicId
                                },
                                success: function(res) {
                                    if(res.result.isLike==1){
                                        that.setData({likeimg:"../../images/已赞.png"});
                                    }else{
                                        that.setData({likeimg:"../../images/未赞.png"});
                                    }
                                },
                                fail: console.error
                            })
                            
                            wx.cloud.callFunction({
                                // 云函数名称
                                name: 'isColl',
                                // 传给云函数的参数
                                data: {
                                    useOpenid:userOpenid,
                                    dynamicId:dynamicId
                                },
                                success: function(res) {
                                    // console.log(res.result.isColl)
                                    if(res.result.isColl==1){
                                        that.setData({collectionimg:"../../images/已收藏.png"});
                                    }else{
                                        that.setData({collectionimg:"../../images/未收藏.png"});
                                    }
                                },
                                fail: console.error
                            })
                        }) 
                    })
                },
                fail: console.error
            })
          },
          judgeBtn(){
            var that=this
            const{dynamicItem}=this.data
            const query = wx.createSelectorQuery().in(this);
            query.selectAll(".dynamictext").boundingClientRect(res => {
                // console.log("测试一下是不是要显示展开收起按钮")
                // console.log(dynamicItem.desc);
                // console.log(res[0]);
                // console.log(res)
                if(res.length>0)
                if(res[0].height>=(30*2+46)){
                    that.setData({dynamicLargeButton:1})
                }else{
                    that.setData({dynamicLargeButton:0})
                }
            }).exec()
            //获取一下用户的信息，判断是不是登录了
            const Info = wx.getStorageSync('userInfo');
            if (Info) {
            this.setData({
                userInfo: Info
            })
        }
          },
    }
})
